package me.green.ui.dialog;

import java.awt.Frame;
import java.awt.Rectangle;
import java.io.File;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import me.green.openswing.JDirChooser;
import me.green.util.DateUtil;
import me.green.util.URLAnalysisUtil;
import com.toedter.calendar.JDateChooser;

public class FileExportDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	public static final int EXPORT_DIARY = 1;
	public static final int EXPORT_PLAN = 2;
	private JPanel jContentPane = null;
	private JComboBox combCondition = null;
	private JDateChooser txtDateTo = null;
	private JDateChooser txtDateFrom = null;
	private JLabel labCondition = null;
	private JLabel labDateFrom = null;
	private JLabel labTo = null;
	private JButton btnExport = null;
	private JTextField txtDir = null;
	private JButton btnChooseDir = null;
	private JLabel labSavePath = null;
	private int exportType = 0;
	private String dirName = "diary";
	private JButton btnDefault = null;

	/**
	 * @param owner
	 */
	public FileExportDialog(Frame owner, boolean model, int exportType) {
		super(owner, model);
		initialize();
		this.exportType = exportType;
		if (exportType == FileExportDialog.EXPORT_PLAN) {
			dirName = "plan";
		}
		this.setLocationRelativeTo(owner);
		this.setVisible(true);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(523, 258);
		this.setTitle("文件导出");
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			labSavePath = new JLabel();
			labSavePath.setBounds(new Rectangle(22, 110, 84, 23));
			labSavePath.setText("保存路径：");
			labTo = new JLabel();
			labTo.setBounds(new Rectangle(311, 65, 36, 22));
			labTo.setText("至");
			labDateFrom = new JLabel();
			labDateFrom.setBounds(new Rectangle(22, 65, 84, 23));
			labDateFrom.setText("时间范围：");
			labCondition = new JLabel();
			labCondition.setBounds(new Rectangle(22, 18, 84, 23));
			labCondition.setText("导出条件：");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getCombCondition(), null);
			jContentPane.add(getTxtDateTo(), null);
			jContentPane.add(getTxtDateFrom(), null);
			jContentPane.add(labCondition, null);
			jContentPane.add(labDateFrom, null);
			jContentPane.add(labTo, null);
			jContentPane.add(getBtnExport(), null);
			jContentPane.add(getTxtDir(), null);
			jContentPane.add(getBtnChooseDir(), null);
			jContentPane.add(labSavePath, null);
			jContentPane.add(getBtnDefault(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes combCondition
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getCombCondition() {
		if (combCondition == null) {
			combCondition = new JComboBox();
			combCondition.setBounds(new Rectangle(123, 15, 149, 29));
			combCondition.addItem("本周记录");
			combCondition.addItem("上周记录");
			combCondition.addItem("所有记录");
			combCondition.addItem("选择时段");
			combCondition
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							combConditionChange();
						}
					});

		}
		return combCondition;
	}

	/**
	 * This method initializes txtDateTo
	 * 
	 * @return javax.swing.JTextField
	 */
	private JDateChooser getTxtDateTo() {
		if (txtDateTo == null) {
			txtDateTo = new JDateChooser();
			txtDateTo.setBounds(new Rectangle(358, 62, 149, 29));
			txtDateTo.setDate(DateUtil.getWeekEnd(DateUtil.getWeekBegin(0)));
			txtDateTo.setEnabled(false);
		}
		return txtDateTo;
	}

	/**
	 * This method initializes txtDateFrom
	 * 
	 * @return javax.swing.JTextField
	 */
	private JDateChooser getTxtDateFrom() {
		if (txtDateFrom == null) {
			txtDateFrom = new JDateChooser();
			txtDateFrom.setBounds(new Rectangle(123, 62, 149, 29));
			txtDateFrom.setDate(DateUtil.getWeekBegin(0));
			txtDateFrom.setEnabled(false);
		}
		return txtDateFrom;
	}

	/**
	 * This method initializes btnExport
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnExport() {
		if (btnExport == null) {
			btnExport = new JButton();
			btnExport.setBounds(new Rectangle(123, 156, 82, 31));
			btnExport.setText("导出");
		}
		return btnExport;
	}

	/**
	 * This method initializes txtDir
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtDir() {
		if (txtDir == null) {
			txtDir = new JTextField();
			txtDir.setBounds(new Rectangle(123, 107, 292, 29));
			txtDir
					.setText(URLAnalysisUtil.getSystemFoderURL() + "\\"
							+ dirName);
		}
		return txtDir;
	}

	/**
	 * This method initializes btnChooseDir
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnChooseDir() {
		if (btnChooseDir == null) {
			btnChooseDir = new JButton();
			btnChooseDir.setBounds(new Rectangle(417, 109, 23, 23));
			btnChooseDir.setText("...");
			btnChooseDir.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					File temp = JDirChooser.showDialog(FileExportDialog.this,
							"浏览目录", true, null, "选择导出的目录");
					if (temp != null) {
						txtDir.setText(temp.getAbsolutePath());
					}
				}
			});
		}
		return btnChooseDir;
	}

	private void combConditionChange() {
		String condition = combCondition.getSelectedItem().toString();
		if (condition.equals("本周记录")) {
			txtDateTo.setEnabled(false);
			txtDateFrom.setEnabled(false);
			txtDateTo.setDate(DateUtil.getWeekEnd(DateUtil.getWeekBegin(0)));
			txtDateFrom.setDate(DateUtil.getWeekBegin(0));
		}
		if (condition.equals("上周记录")) {
			txtDateTo.setEnabled(false);
			txtDateFrom.setEnabled(false);
			txtDateTo.setDate(DateUtil.getWeekEnd(DateUtil.getWeekBegin(-1)));
			txtDateFrom.setDate(DateUtil.getWeekBegin(-1));
		}
		if (condition.equals("所有记录")) {
			txtDateTo.setEnabled(false);
			txtDateFrom.setEnabled(false);
			txtDateTo.setDate(new Date());
			txtDateFrom.setDate(new Date());
		}
		if (condition.equals("选择时段")) {
			txtDateTo.setEnabled(true);
			txtDateFrom.setEnabled(true);
			txtDateTo.setDate(new Date());
			txtDateFrom.setDate(new Date());
		}
	}

	/**
	 * This method initializes btnDefault
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnDefault() {
		if (btnDefault == null) {
			btnDefault = new JButton();
			btnDefault.setBounds(new Rectangle(444, 109, 63, 23));
			btnDefault.setText("默认");
			btnDefault.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					txtDir.setText(URLAnalysisUtil.getSystemFoderURL() + "\\"
							+ dirName);
				}
			});
		}
		return btnDefault;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
